import java.io.*;
/**
 * Saves the objects to a file and loads
 * them back in the same order
 * Note: Might implement Serialiazable to make it better
 * ^ probably not.
 * @author Sparsh
 *
 */

public class SaveLoad {
	Ship ship;
    Timeline timeline;
    
	SaveLoad(Ship s, Timeline t){
		ship = s;
        timeline = t;
	}
	/**
	 * Saves the objects
	 */
	public void save(File dir){
		try{  
			  System.out.println("saving to: "+dir+"/Save.sav");
		      FileOutputStream saveFile=new FileOutputStream(dir+"\\Save.sav");
		      ObjectOutputStream save = new ObjectOutputStream(saveFile);
		      save.writeObject(ship.getCaptainName());
		      save.writeObject(ship.getSaveCrew());
		      save.writeObject(ship.getItems());
		      save.writeObject(ship.getPace());
		      save.writeObject(ship.getProf());
		      save.writeObject(ship.getRations());
		      save.writeObject(timeline.getCurrentDist());
		      save.writeObject(timeline.getCurrentStep());
		      save.writeObject(timeline.getDistToNext());
		      save.writeObject(timeline.getTotalDist());
		      save.close();
		      System.out.println("Saved");		  
		      System.out.println(ship.getCaptainName()+" " + ship.getPace() + " " + ship.getProf() +  " "
						 + ship.getRations() + " " + timeline.getDistToNext()+" " + timeline.getTotalDist() + " " 
						 + timeline.getCurrentDist() + timeline.getCurrentStep() + " " + ship.getCrewNames());
		      
		    }
		    catch(Exception exc){
		      exc.printStackTrace();
		    }
	}

	/**
	 * Loads the objects back
	 */
	public void load(File loadFile){
		 try{
			   FileInputStream saveFile = new FileInputStream(loadFile);
			   ObjectInputStream save = new ObjectInputStream(saveFile);
			   ship.setCaptainName((String) save.readObject());
			   ship.setSaveCrew((String[]) save.readObject());
			   ship.setItems((int[]) save.readObject());
			   ship.setPace((Integer) save.readObject());
			   ship.setProf((String) save.readObject());
			   ship.setRations((Integer)save.readObject());
			   timeline.setCurrentDist((Integer)save.readObject());
			   timeline.setCurrentStep((Integer)save.readObject());
			   timeline.setDistToNext((Integer)save.readObject());
			   timeline.setTotalDist((Integer)save.readObject());
			   save.close(); 
			   System.out.println("Loaded");
			   System.out.println(ship.getCaptainName()+" " + ship.getPace() + " " + ship.getProf() +  " "
						 + ship.getRations() + " " + timeline.getDistToNext()+" " + timeline.getTotalDist() + " " 
						 + timeline.getCurrentDist() + timeline.getCurrentStep() + " " + ship.getCrewNames());
			   
			   //jons debugging
			   System.out.println(timeline.currentState());
				
		 }
			  catch(Exception exc){
			   exc.printStackTrace(); 
			  }
		}
	
}
